<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of news_model
 *
 * @author Administrator
 */
class news_model extends CI_Model{
    //put your code here
    function __construct() {
        parent::__construct();
    }
    
    function getLatestNews(){
        $query=$this->db->get(tbl_news);
        $this->db->order_by('news_date');
        if($query->num_rows<=0)
            return null;
        $arr=$query->result();
        return $arr[0];
    }
    
    function getAllNews($limit){
        $this->db->limit($limit);
        $query=$this->db->get(tbl_news);
        return $query->result();
        
    }
    
    function getNewsByID($id){
        $query=$this->db->get_where(tbl_news,array('news_id'=>$id,'news_is_delete'=>0));
        if($query->num_rows<=0)
            return null;
        $arr=$query->result();
        return $arr[0];
    }
    
    function getAllNews_Admin($page = 0, $page_size = PAGE_SIZE){
        $this->db->limit($page_size, $page);
        $this->db->select('n.*');
        $this->db->from(tbl_news.' n');
        $this->db->where('news_is_delete', 0);
        $this->db->order_by('news_date','desc');
        $query = $this->db->get();


        //Get total rows
        $this->db->select('news_id');
        $this->db->from(tbl_news);
        $this->db->where('news_is_delete', 0);
        $total = $this->db->count_all_results();

        $data = array('list' => $query->result(),
            'total' => $total);
        return $data;
    }
    
    function addNews($info){
        return $this->db->insert(tbl_news,$info);
    }
    
    function updateNews($info,$id){
        $this->db->where('news_id',$id);
        return $this->db->update(tbl_news,$info);
    }
    
    function deleteNews($id){
        $this->db->where('news_id',$id);
        return $this->db->update(tbl_news,array('news_is_delete'=>1));
    }
}   

?>
